#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

#define N 1024

int main() {
    // 原始写入端代码（唯一main函数）
    int fd;
    char buf[N];
    
    printf("Writer opening...\n");
    if ((fd = open("/home/mypipe", O_WRONLY)) == -1) {
        perror("open failed");
        exit(EXIT_FAILURE);
    }

    printf("Enter message: ");
    fgets(buf, N, stdin);
    ssize_t bytes = write(fd, buf, strlen(buf));
    if (bytes == -1) {
        perror("write failed");
    } else {
        printf("Wrote %zd bytes\n", bytes);
    }

    close(fd);
    return EXIT_SUCCESS;
}
